/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import io.renren.annotation.ApiAuth;
import io.renren.annotation.ApiSignAuth;
import io.renren.annotation.Login;
import io.renren.annotation.LoginUser;
import io.renren.common.utils.DateUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.config.ApiConfig;
import io.renren.entity.UserEntity;
import io.renren.form.LoginForm;
import io.renren.form.RegisterForm;
import io.renren.service.UserService;
import io.renren.util.ApiResult;
import io.renren.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api")
@Api(tags = "api测试接口")
public class ApiTestController extends ApiBaseController {


    @Autowired
    private UserService userService;


    @PostMapping("login")
    @ApiOperation("登录")
    public ApiResult login(@RequestBody LoginForm form) {
        //表单校验
        ValidatorUtils.validateEntity(form);

        //用户登录
        Map<String, Object> map = userService.login(form);

        return success(map);
    }

    @Login
    @PostMapping("logout")
    @ApiOperation("退出")
    public ApiResult logout(@ApiIgnore @RequestAttribute("userId") long userId) {
        return success();
    }

    @PostMapping("register")
    @ApiOperation("注册")
    public ApiResult register(@RequestBody RegisterForm form) {
        //表单校验
        ValidatorUtils.validateEntity(form);

        UserEntity user = new UserEntity();
        user.setMobile(form.getMobile());
        user.setUsername(form.getMobile());
        user.setPassword(DigestUtils.sha256Hex(form.getPassword()));
        user.setCreateTime(new Date());
        userService.save(user);

        return success();
    }

    /**
     * 通过参数切面获取登录用户对象
     */
    @Login
    @GetMapping("userInfo")
    @ApiOperation(value = "获取用户信息", response = UserEntity.class)
    public ApiResult userInfo(@ApiIgnore @LoginUser UserEntity user) {
        return success("user", user);
    }

    /**
     * 获取在拦截器中存入的登录用户ID
     */
    @Login
    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public ApiResult userInfo(@ApiIgnore @RequestAttribute("userId") Integer userId) {
        return success("userId", userId);
    }

    /**
     * 跳过Token
     */
    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public ApiResult notToken() {
        return success("无需token也能访问。。。");
    }

    /**
     * 创建Token
     */
    @GetMapping("token")
    public ApiResult token() {
        String uid = getParameter("uid");
        //获取token
        String token = JwtUtil.createToken(uid, ApiConfig.TOKEN_KEY,
                ApiConfig.TOKEN_SECRET, ApiConfig.TOKEN_EXPIRE_TIME);

        Map<String, Object> map = new HashMap<>(2);
        map.put("token", token);
        map.put("expire", DateUtils.format(new Date(System.currentTimeMillis() + ApiConfig.TOKEN_EXPIRE_TIME), DateUtils.DATE_TIME_PATTERN));

        return success(map);
    }

    /**
     * 通过Token获取用户ID
     */
    @ApiAuth
    @PostMapping("queryUserIdByToken")
    public ApiResult queryUserIdByToken() {
        String uid = getUserId();
        return success(uid);
    }


    /**
     * sign验证
     */
    @ApiSignAuth
    @GetMapping("testSignAuth")
    @ApiOperation("sign验证")
    public ApiResult testSignAuth() {
        return success("msg", "需要sign参数访问");
    }

    /**
     * 跳过sign验证
     */
    @ApiSignAuth(isAuth = false)
    @GetMapping("testNotSignAuth")
    @ApiOperation("忽略Token验证测试")
    public ApiResult testNotSignAuth() {
        return success("msg", "无需sign参数也能访问");
    }


    @ApiSignAuth(isAuth = false)
    @GetMapping("testUserCache")
    @ApiOperation("测试缓存")
    public ApiResult testUserCache() {
        String mobile = getParameter("mobile");
        Object obj = userService.queryByMobile(mobile);
        return success(obj);
    }

    @ApiSignAuth(isAuth = false)
    @GetMapping("testUserCleanCache")
    @ApiOperation("测试清除缓存")
    public ApiResult testUserCleanCache() {
        String mobile = getParameter("mobile");
        userService.cleanCacheQueryByMobile(mobile);
        return success();
    }

}
